// 发布商品界面

const db = wx.cloud.database()

Page({
  data: {
    uploadPhotoList: [],
    photosFileID: [],
    campusArray: ["江安校区", "望江校区", "华西校区"],
    campusIndex:0,
    sortArray: ["图书", "数码", "生活","化妆品"],
    sortIndex:0,

    num: 0,
    title:"",
    des:"",
    price:"",
    qq:"",
    wx:"",
  },
  //上传图片
  uploadPhoto: function(e) {
    wx.chooseImage({
      count: 3,   
      sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        this.setData({
          uploadPhotoList: res.tempFilePaths,
          num: res.tempFilePaths.length
        })    
      }
    })
  },

  // 删除图片
  clearImg:function(e){
    var nowList = [];//新数据
    var uploadPhotoList = this.data.uploadPhotoList;//原数据
    
    for (let i = 0; i < uploadPhotoList.length; i++){
        if (i == e.currentTarget.dataset.index){
            continue;
        }else{
            nowList.push(uploadPhotoList[i])
        }
    }
    this.setData({
        uploadPhotoList: nowList,
    })
  },

  //单选框改变值campus
  bindPickerChangeCampus: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      campusIndex: e.detail.value
    })
  },

  //单选框改变值sort
  bindPickerSort: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      sortIndex: e.detail.value
    })
  },

   //获取标题
   getTitle:function(e){
    this.setData({
      title: e.detail.value
    })
  },

  //获取详情
  getDes:function(e){
    this.setData({
      des: e.detail.value
    })
  },

  //获取价格
  getPrice:function(e){
    this.setData({
      price: e.detail.value
    })
  },

  //获取qq
  getQQ:function(e){
    this.setData({
      qq: e.detail.value
    })
  },

  //获取wx
  getWX:function(e){
    this.setData({
      wx: e.detail.value
    })
  },

  //图片存入云存储
  release(){
    //上传图片的部分
    var that = this
    var filePath=that.data.uploadPhotoList
    filePath.forEach((element,index) => {
      //图片名字，时间戳+index
      var fliename = Date.now()+"_"+index
      //上传图片
      wx.cloud.uploadFile({
        // cloudPath: "/goods/" + fliename + ".png",
        cloudPath: "goods/" + fliename + ".png",
        filePath: element})
        .then(res=>{
          //console.log('语句1',res.fileID)
          that.data.photosFileID.push(res.fileID)
          //console.log('语句2',that.data.photosFileID)
          if(that.data.photosFileID.length == that.data.num){
            that.uploadDB()
            console.log("发布成功")
            wx.switchTab({
              url: '../../pages/index/index',
            })
          }
        })
    })
  },

  //图片fileID存入数据库
  uploadDB(){
    db.collection("goods").add({
      data: {
        content: this.data.des,
        name: this.data.title,
        price: this.data.price,
        time: Date.now(),
        photos: this.data.photosFileID,
        qq: this.data.qq,
        wx: this.data.wx,
        campus: this.data.campusArray[this.data.campusIndex],
        type: this.data.sortArray[this.data.sortIndex]
      }
    })
    .then(res =>{
      console.log('语句4',res)
    })
  }

})